/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.api;

import com.arisgo.cloud.attendance.clazz.service.StaffClassService;
import com.arisgo.cloud.attendance.clazz.vo.ClassInfoDetailVo;
import com.arisgo.cloud.attendance.clazz.vo.StaffClassVo;
import com.arisgo.cloud.common.Pagination;
import com.arisgo.cloud.common.Result;
import com.arisgo.cloud.common.dynamic.Table;
import com.arisgo.cloud.web.param.Paging;
import com.arisgo.cloud.web.param.Search;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * Api
 *
 * @author Coulson
 * @since 2024-12-31
 */
@RestController
@AllArgsConstructor
@RequestMapping
@Tag(name = " API", description = "CRUD操作")
public class StaffClassApi {

    private final StaffClassService staffClassService;

    /**
     * 依id查询
     */
    @GetMapping(value = "/class/staff/{id}")
    @Operation(summary = "依id查询")
    public Result<StaffClassVo> findById(@PathVariable(value = "id") Long id) {
        return Result.success(staffClassService.findById(id));
    }

    /**
     * 分页查询
     */
    @PostMapping(value = "/class/staff/page")
    @Operation(summary = "分页查询")
    public Result<Pagination<StaffClassVo>> page(@RequestBody Paging paging) {
        return Result.success(staffClassService.page(paging));
    }

    /**
     * 条件查询
     */
    @PostMapping(value = "/class/staff/find")
    @Operation(summary = "条件查询")
    public Result<List<StaffClassVo>> find(@RequestBody Search search) {
        return Result.success(staffClassService.findByParams(search));
    }

    /**
     * 保存
     */
    @PostMapping(value = "/class/staff")
    @Operation(summary = "新增数据")
    public Result<StaffClassVo> save(@RequestBody StaffClassVo vo) {
        return Result.success(staffClassService.save(vo));
    }

    /**
     * 更新
     */
    @PutMapping(value = "/class/staff/{id}")
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@PathVariable(value = "id") Long id, @RequestBody StaffClassVo vo) {
        vo.setId(id);
        return Result.success(staffClassService.update(vo));
    }

    /**
     * 物理删除
     */
    @DeleteMapping(value = "/class/staff/remove/{id}")
    @Operation(summary = "单个物理删除")
    public Result<Boolean> remove(@PathVariable(value = "id") Long id) {
        return Result.success(staffClassService.remove(id));
    }

    /**
     * 物理删除
     */
    @DeleteMapping(value = "/class/staff/remove")
    @Operation(summary = "批量物理删除")
    public Result<Boolean> remove(@RequestBody List<Long> ids) {
        return Result.success(staffClassService.remove(ids));
    }


    @PostMapping(value = "/class/{classId}/staff")
    @Operation(summary = "依班次id查询员工")
    public Result<Pagination<StaffClassVo>> findStaffByClassId(@PathVariable(value = "classId") Long classId,
                                                               @RequestBody Paging paging) {
        return Result.success(staffClassService.findStaffByClassId(classId, paging));
    }

    /**
     * 查询员工排班情况
     * <p>
     * 依时间段分页查询
     * 默认查询当前周的员工排班
     * 通过入参的不同来控制
     */
    @PostMapping(value = "/staff/class/page")
    public Result<Table<Pagination<?>>> pageStaffClass(@RequestParam(value = "tenantId") Long tenantId,
                                                       @RequestParam(value = "start", required = false) LocalDate start,
                                                       @RequestParam(value = "end", required = false) LocalDate end,
                                                       @RequestBody Paging paging) {
        return Result.success(staffClassService.pageStaffClass(tenantId, start, end, paging));
    }

    @PostMapping(value = "/staff/class/week")
    public Result<Table<Pagination<?>>> pageStaffWeek(@RequestParam(value = "tenantId") Long tenantId,
                                                      @RequestParam(value = "date", required = false) LocalDate date,
                                                      @RequestBody Paging paging) {
        return Result.success(staffClassService.pageStaffWeek(tenantId, date, paging));
    }

    /**
     * 查询员工某天班次详情
     */
    @GetMapping(value = "/staff/{staffId}/class")
    public Result<List<ClassInfoDetailVo>> findClassByStaff(@PathVariable(value = "staffId") Long staffId,
                                                            @RequestParam(value = "date") LocalDate date) {
        return Result.success(staffClassService.findClassDetailByStaff(staffId, date));
    }

}
